package com.itheima.feiqiu;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/*
飞秋
 */
public class FeiQiu增强 {

    public static void main(String[] args) throws Exception {
        DatagramSocket ds = new DatagramSocket();
        new Thread(()-> {
            while (true) {
                try {
                    String msg = new Scanner(System.in).nextLine();
                    String str = "1:100:翟超杰:teacher_zhai:32:"+msg;//按照飞鸽传书协议的格式
                    InetAddress ip = InetAddress.getByName("192.168.23.1");//请求的ip
                    int port = 2425;//飞秋软件端口号
                    DatagramPacket p = new DatagramPacket(str.getBytes("GBK"), str.getBytes("GBK").length,ip,port);//将数据报打包
                    ds.send(p);//发送
                } catch (IOException e) {
                    e.printStackTrace();
                }

                System.out.println("************已发送");
            }
        }) .start();

        new Thread(()->{
            DatagramPacket p = new DatagramPacket(new byte[1024], new byte[1024].length);//接收 ,最大64k
            while(true){
                try {
                    ds.receive(p);
                    String re = new String(p.getData(),"GBK");
                    String[] split = re.split(":");
                    System.out.println(split[split.length-1]);
                   // System.out.println(re+"*********************");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }


}
